define(function(require){
	var $ = require("jquery");
	var justep = require("$UI/system/lib/justep");
	var MD5 = require('$UI/system/lib/base/md5');	
	var md5 = new MD5();
	
	var Model = function(){
		this.callParent();
	};
	
	//图片路径转换
	Model.prototype.toUrl = function(url){
		return url ? require.toUrl(url) : "";
	};

	Model.prototype.loginBtnClick = function(event){
		this.login();
	};
	Model.prototype.login = function(){
		//debugger;
		var //remember = this.comp('remember'),
			name = this.comp("dataLogin").getValue("userName"),
			password = this.comp("dataLogin").getValue("password");
			//autoLogin = this.comp('autoLogin');
	
        var urlParams = {};
        urlParams.username = name;//为了支持360急速浏览器, 自动设置参数的情况
        urlParams.password = password;//同上
        //urlParams.password = urlParams.password;
    	urlParams.loginDate = justep.Date.toString(new Date(), 'yyyy-MM-dd');
        
        if(!urlParams.username) {
        	justep.Util.hint('用户名不能为空！');
        	return;
        	}
        
        var result = this.doLogin(urlParams);
        
        if(result.flag === true){
        
			var auto = true;//autoLogin.val();
			if(true/*remember.val()*/){
				//this.store('rememberme',"remember");
				this.store('username',urlParams.username);
				this.store('password',urlParams.password);
				//this.store('truename',result.truename);
				//this.store('autoLogin', auto);
				this.store('level', result.level);
				this.store('lastLoginTime', urlParams.loginDate);
            }
			
			//TODO 对话框返回数据,根据需要可以添加需要的数据返回给门户
			this.comp("receiver").windowEnsure(result);
			
            //return false;
        }else{
            justep.Util.hint(result.message);
        }
	};
	Model.prototype.doLogin = function(params){
		//TODO 开发者实现登录逻辑		
		var result = false;
		var trueName = "";
		var message = "";
		//debugger;
		var userDatas = this.comp("userData").find(["fUserName"],[params.username]);
		if(userDatas.length > 0){
			var password = userDatas[0].val("fUserPassword");
			if(password == md5.hex_md5(params.password)){
				result = true;
				trueName = userDatas[0].val("fTrueName");
			}
			else{
				result = false;
				message = "用户名密码错误";
			}
		}
		else{
			result = false;
			message = "没有找到该用户";
		}
		return {flag:result,mod:"login",username:params.username,truename:trueName,message:message};
    };	
	Model.prototype.store = function(name, value){
		if(value !== undefined){
			localStorage.setItem(name, value);		
		}else{
			value = localStorage.getItem(name);
			if(value === 'true') value = true;
			if(value === 'false') value = false;
			if(value === 'null') value = null;
			return value;
		}
	};
	Model.prototype.registerBtnClick = function(event){
		var result= {flag:false,mod:"register"};
		this.comp("receiver").windowEnsure(result);

	};
	Model.prototype.forgetBtnClick = function(event){
		justep.Util.hint("请致电海锝获取重置密码服务");
		var result= {flag:false,mod:"forget"};
		//this.comp("receiver").windowEnsure(result);

	};
	Model.prototype.modelLoad = function(event){
		var name = this.store('username');
		var pass = this.store('password');
		this.comp("dataLogin").setValue("userName", name);
		this.comp("dataLogin").setValue("password", pass);
		//this.comp("nameInput").set({value:name});
		//this.comp("passwordInput").set({value:pass});
	};
	return Model;
});